//
//  ZDAccessoryButton.m
//  1012GiftTips
//
//  Created by Apple on 15/10/21.
//  Copyright © 2015年 itcast. All rights reserved.
//

#import "ZDAccessoryButton.h"
#import "UIView+ZUtility.h"


@interface ZDAccessoryButton ()

@property (nonatomic, assign) CGFloat imageW;

@property (nonatomic, assign) CGFloat margin;

@end


@implementation ZDAccessoryButton

#pragma mark - 第一种方式调整button中子控件的位置的大小
//重新布局子控件的frame属性
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    //定义按钮中文本域图片之间的间距
    CGFloat margin = 5;
    
    CGFloat titleW = self.titleLabel.width;
    CGFloat titleH = self.titleLabel.height;
    CGFloat imageW = self.imageView.width;
    CGFloat imageH = self.imageView.height;
    
    
    if (!self.currentTitle)
    {
        self.titleLabel.frame = CGRectZero;
    }
    else
    {
        CGFloat titleX = self.width - margin - imageW - titleW;
        CGFloat titleY = (self.height - titleH) * 0.5;
        
        self.titleLabel.frame = CGRectMake(titleX, titleY, titleW, titleH);
    }
    
    CGFloat imageX = self.width - imageW;
    CGFloat imageY = (self.height - imageH) * 0.5;
    
    self.imageView.frame = CGRectMake(imageX, imageY, imageW, imageH);
}


//- (void)setTitle:(NSString *)title forState:(UIControlState)state
//{
//    [super setTitle:title forState:state];
//    
//    [self setNeedsLayout];
//}


#pragma mark - 第二种方式调整button中子控件的位置的大小
//- (void)awakeFromNib
//{
//    [super awakeFromNib];
//
//    self.imageW = self.currentImage.size.width;
//    self.margin = 5;
//}
//
//- (CGRect)titleRectForContentRect:(CGRect)contentRect
//{
#warning 不能直接使用self.titleLabel.frame, 因为self.titleLabel.frame内部会调用titleRectForContentRect:方法，这会导致死循环
//    CGFloat titleW = [self.currentTitle boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:CZFont(18)} context:nil].size.width;
//    CGFloat titleH = contentRect.size.height;
//    CGFloat titleX = (contentRect.size.width - titleW - self.margin - self.imageW) * 0.5;
//    CGFloat titleY = 0;
//
//    return CGRectMake(titleX, titleY, titleW, titleH);
//}
//
//- (CGRect)imageRectForContentRect:(CGRect)contentRect
//{
#warning 不能直接使用self.imageView.frame, 因为self.imageView.frame内部会调用imageRectForContentRect:方法，这会导致死循环
//    CGFloat imageW = self.currentImage.size.width;
//    CGFloat imageH = self.currentImage.size.height;
//    CGFloat imageX = CGRectGetMaxX(self.titleLabel.frame) + self.margin;
//    CGFloat imageY = (contentRect.size.height - imageH) * 0.5;
//
//    return CGRectMake(imageX, imageY, imageW, imageH);
//}


@end
